package kr.hkit.powerjava.chther26.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;


import kr.hkit.powerjava.chther26.dao.DepartmentDAO;
import kr.hkit.powerjava.chther26.dto.Department;

public class DepartmentService extends AbsService<Department> {
	private ArrayList<Department> list;
	public DepartmentService(Scanner sc,Connection connection) {
		super(sc,connection);
		dao = new DepartmentDAO(connection);
		
	}
	@Override
	public Department inputItemInfo(boolean gubun) {
		System.out.println("부서번호 부서명");
		Department dept = new Department(sc.nextInt(), sc.next().trim());
		if (gubun) {
			System.out.println("위치");
			int floor = sc.nextInt();
			dept.setFloor(floor);
		}
		return dept;
	}

	@Override
	public void prnSubMenu() {
		System.out.print("1. 부서순 정렬 2. 위치별 정렬");
		try {
			list = dao.listItems();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int res = sc.nextInt();
		switch (res) {
			case 1:
				
				prnItemList(new Comparator<Department>() {
					
					@Override
					public int compare(Department o1, Department o2) {
						// TODO Auto-generated method stub
						return o1.getDeptName().hashCode()-o2.getDeptName().hashCode();
					}
				});
				break;
			case 2:
				prnItemList(new Comparator<Department>() {
					@Override
					public int compare(Department o1, Department o2) {
						// TODO Auto-generated method stub
						return o1.getFloor()-o2.getFloor();
					}
				});
				break;

			default:
				prnItemList(null);
				
		}
		
	}
	private void prnItemList(Comparator<Department> comparator) {
		if(comparator == null){
			Collections.sort(list);
			prnlist(list);
		}else{
			Collections.sort(list,comparator);
		}
		prnlist(list);
	}
	private void prnlist(ArrayList<Department> list) {
		for(Department d:list){
			System.out.println(d);
		}
	}

	
	
}
